define(["jquery","dg/datagride.config","dg/datagride.common"],function($,$config,$common){
	var servers = {};

	/********************** table head begin **********************/

	// 格式化label
	var formatLabel = function(label){
		if( typeof label == "string" ){
			var str = label;
			label = {};
			label.value = str;
		}else if( typeof label == "object" ){
			var styleObj = $common.getStyle(label);
			$.extend(label,styleObj);
		}
		return label;
	}
	// 获取该组题型下拥有多少叶子
	var getFlexLeaf = function(arr,num){
		if( !num ) num = 0;
		for (var i = 0; i < arr.length; i++) {
			if( arr[i].children ){
				num = getFlexLeaf(arr[i].children,num);
			}else{
				num ++;
			}
		}
		return num;
	}
	// 格式化head
	// flex: 表头在top方时，需要
	var formatHead = function(arr,direction){
		for (var i = 0; i < arr.length; i++) {
			// 题型公有属性处理
			arr[i].label = formatLabel(arr[i].label);
			if( arr[i].children ) {
				if( direction == "top" ) {
					arr[i].flex = arr[i].children.length;
					arr[i].flexLeaf = getFlexLeaf(arr[i].children);
				}
				arr[i].children = formatHead(arr[i].children,direction);
			}else{
				if( direction == "top" ) arr[i].flex = 1;
			}
			// 题型私有属性处理(在每个题型里单个处理)
			if( arr[i].type ){
				var $quest = $config.getQuest(arr[i].type);
				if( $quest.formatData ) {
					if( typeof $quest.formatData == "object" ){
						if( $quest.formatData.head && typeof $quest.formatData.head == "function" ) 
						arr[i] = $quest.formatData.head(arr[i]);
					}
				}
			}
		}
		return arr;
	} 
	// 获取原始表头数据
	var gethHeadData = function(data,title){
		var arr = [];
		for (var i = 0; i < data.length; i++) {
			arr[i] = data[i];
		}
		arr.unshift(title);
		return arr;
	}
	/**
	 * 格式化head数据
	 */
	servers.formatHeadData = function(questions){
		if( questions.yAxis.type == "category" ){
			var direction = "left";
			var data = questions.yAxis.data;
			
		}else if( questions.xAxis.type == "category" ){
			var direction = "top";
			var data = questions.xAxis.data;
		}
		var headData = gethHeadData(data,questions.title);
		var handleHeadData = formatHead(headData,direction);
		return {
			"heads":{ "children":handleHeadData },
			"direction":direction
		};
	}

	/********************** table content begin **********************/

	/**
	 * 获取处理后的value
	 * @param  {[obj]} obj   [配置的题型]
	 * @param  {[num]} i     [第i次记录]
	 * @param  {[str]} value [value值]
	 * @return {[obj]} newObj[配置的题型里需要的以及处理后的字段]
	 */
	var getHandleValue = function(obj,i,value){
		var newObj = $.extend({},obj);
		// 题型公有属性处理
		if( newObj.value ){
			var styleObj = $common.getStyle(newObj.value,i,value);
			$.extend(newObj,styleObj);
		}
		if( newObj.attention && typeof newObj.attention != "string" ){
			newObj.attention = JSON.stringify(newObj.attention);
		}
		delete newObj.id;
		delete newObj.label;
		delete newObj.value;
		// 题型私有属性处理(在每个题型里单个处理)
		var $quest = $config.getQuest(newObj.type);
		if( $quest.formatData ) {
			if( typeof $quest.formatData == "function" ){
				newObj = $quest.formatData(newObj); 
			}else if( typeof $quest.formatData == "object" ){
				if( $quest.formatData.content && typeof $quest.formatData.content == "function" ) 
				newObj = $quest.formatData.content(newObj); 
			}
		}
		return newObj;
	}

	// 格式化contnt数据
	var formatContent = function(xData,values){
		for (var i = 0; i < xData.length; i++) {
			if( xData[i].children ){
				xData[i].children = formatContent(xData[i].children,values);
			}else{
				for (var j = 0; j < values.length; j++) {
					for (var k = 0; k < values[j].details.length; k++) {
						if( xData[i].id == values[j].details[k].questionId ){
							var obj = getHandleValue(xData[i],j,values[j].details[k].value);
							$.extend(values[j].details[k],obj,{"recordId":values[j].recordId});
						}
					}
				}
			}
		}
		return xData;
	}

	/**
	 * 格式化content数据
	 */
	servers.formatContentData = function(questions,values){
		if( questions.yAxis.type == "category" ){
			var direction = "left";
			var data = questions.yAxis.data;
		}else if( questions.xAxis.type == "category" ){
			var direction = "top";
			var data = questions.xAxis.data;
		}
		formatContent(data,values);
		return {
			"contents":values,
			"direction":direction
		};
	}

	return servers;
});